package com.xx.xunyou.serviceImpl;

import com.xx.springBootUtil.excption.BaseException;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.xunyou.dao.item.youlezaixiangcun.YoulezaixiangcunDAO;
import com.xx.xunyou.dao.item.youlezaixiangcun.YoulezaixiangcunImageDAO;
import com.xx.xunyou.entity.item.chizhuzaixiang.vo.ChizhuzaixiangVo;
import com.xx.xunyou.entity.item.youlezaixiangcun.Youlezaixiangcun;
import com.xx.xunyou.entity.item.youlezaixiangcun.YoulezaixiangcunImage;
import com.xx.xunyou.entity.item.youlezaixiangcun.vo.YoulezaixiangcunVo;
import com.xx.xunyou.service.YoulezaixiangcunSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: xunyou-parent
 * @author: zhl
 * @create: 2019-04-18
 **/
@Service
public class YoulezaixiangcunSerivceImpl implements YoulezaixiangcunSerivce {

    @Autowired
    private YoulezaixiangcunDAO youlezaixiangcunDAO;

    @Autowired
    private YoulezaixiangcunImageDAO youlezaixiangcunImageDAO;

    @Override
    public Integer queryListCountToManager(Map<String, Object> map) {
        return youlezaixiangcunDAO.queryListCountToManager(map);
    }

    @Override
    public List<Map> queryListToManager(Map<String, Object> map) {
        return youlezaixiangcunDAO.queryListToManager(map);
    }

    @Override
    @Transactional
    public void insert(Youlezaixiangcun youlezaixiangcun, String images) {
        Date date = new Date();
        youlezaixiangcun.setCreateTime(date);
        String[] split = images.split(",");
        if (ObjectHelper.isEmpty(split)){
            throw new BaseException("图片必传！");
        }
        youlezaixiangcun.setCover(split[0]);
        youlezaixiangcunDAO.insertBackId(youlezaixiangcun);
        List<YoulezaixiangcunImage> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            YoulezaixiangcunImage youlezaixiangcunImage = new YoulezaixiangcunImage();
            youlezaixiangcunImage.setCreateTime(date);
            youlezaixiangcunImage.setImageUrl(split[i]);
            youlezaixiangcunImage.setYoulezaixiangcunId(youlezaixiangcun.getYoulezaixiangcunId());
            list.add(youlezaixiangcunImage);
        }
        youlezaixiangcunImageDAO.insertList(list);
    }

    @Override
    public Youlezaixiangcun queryByIdToManager(Integer youlezaixiangcunId) {
        return youlezaixiangcunDAO.selectByPrimaryKey(youlezaixiangcunId);
    }

    @Override
    @Transactional
    public void update(Youlezaixiangcun youlezaixiangcun, String images) {
        String[] split = images.split(",");
        if (ObjectHelper.isEmpty(split)){
            throw new BaseException("图片必传！");
        }
        youlezaixiangcun.setCover(split[0]);
        youlezaixiangcunDAO.updateByPrimaryKeySelective(youlezaixiangcun);
        youlezaixiangcunImageDAO.deleteByYoulezaixiangcunId(youlezaixiangcun.getYoulezaixiangcunId());
        Date date = new Date();
        List<YoulezaixiangcunImage> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            YoulezaixiangcunImage youlezaixiangcunImage = new YoulezaixiangcunImage();
            youlezaixiangcunImage.setCreateTime(date);
            youlezaixiangcunImage.setImageUrl(split[i]);
            youlezaixiangcunImage.setYoulezaixiangcunId(youlezaixiangcun.getYoulezaixiangcunId());
            list.add(youlezaixiangcunImage);
        }
        youlezaixiangcunImageDAO.insertList(list);
    }

    @Override
    public List<YoulezaixiangcunImage> queryImagesByIdToManager(Integer youlezaixiangcunId) {
        return youlezaixiangcunImageDAO.queryImagesByIdToManager(youlezaixiangcunId);
    }

    @Override
    @Transactional
    public void delete(Integer youlezaixiangcunId) {
        youlezaixiangcunDAO.deleteByPrimaryKey(youlezaixiangcunId);
        youlezaixiangcunImageDAO.deleteByYoulezaixiangcunId(youlezaixiangcunId);
    }

    @Override
    public void recommend(Integer youlezaixiangcunId) {
        youlezaixiangcunDAO.recommend(youlezaixiangcunId,new Date());
    }

    @Override
    public void notRecommend(Integer youlezaixiangcunId) {
        youlezaixiangcunDAO.notRecommend(youlezaixiangcunId);
    }

    @Override
    public List<YoulezaixiangcunVo> queryListToAPPIndex(Map<String, Object> map) {
        return youlezaixiangcunDAO.queryListToAPPIndex(map);
    }

}
